﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace SerbinAV.MobileSales.Sales.Entities
{
    public class Visit
    {
        public delegate List<Order> OrderOnVisit(int idVisit);
        public event OrderOnVisit LoadOrder;

        public delegate BuyPoint BuyPointOnId(int idbuyPoint);
        public event BuyPointOnId LoadBuyPoint;

        public int IdVisit { get; set; }
        public bool IsExport { get; set; }
        public DateTime? TimeEndVisit { get; set; }
        public DateTime DateVisit { get; set; }
        public string GpsData { get; set; }
        public int? BuyPointId { get; set; }
        public bool? IsOrder { get; set; }

        public bool IsVisit
        {
            get
            {
                return TimeEndVisit != null;
            }
        }

        private List<Order> _orders;
        public List<Order> Orders
        {
            get
            {
                if (_orders == null)
                {
                    if (IsOrder == true && LoadOrder != null)
                        _orders = LoadOrder(IdVisit);
                    else
                    {
                        _orders = new List<Order>();
                    }
                }
                return _orders;
            }
            set { _orders = value; }
        }
        
        private BuyPoint _buyPoint;
        public BuyPoint BuyPoint
        {
            get
            {
                if (_buyPoint == null) 
                {
                    if (BuyPointId != null && LoadBuyPoint != null)
                        _buyPoint = LoadBuyPoint(BuyPointId.Value);
                    else
                        _buyPoint = new BuyPoint();
                }
                return _buyPoint;
            }
            set
            {
                _buyPoint = value;
            }
        }

        public string ByPointName
        {
            get
            {
                return BuyPoint.NameBuyPoint;
            }
        }
        public string BuyPointAdress
        {
            get
            {
                return BuyPoint.Adress;
            }
        }
        public double BuyPointDebts
        {
            get { return BuyPoint.Debts; }
        }
    }
}
